package com.relife.core.aop;

import java.io.Serializable;
import java.util.Date;

import com.relife.common.model.Client;
import com.relife.common.model.ClientManager;
import com.relife.common.utils.SysConfig;
import com.relife.core.entity.LogEntity;
import com.relife.core.service.LogService;
import com.relife.core.spring.SpringContextUtil;
import org.apache.log4j.Logger;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;



/**
 * 
 * @author WU
 * @date 2015-4-2 下午9:28:53
 */
@Component("hiberAspect")
public class HiberAspect extends EmptyInterceptor {
	private static final Logger logger = Logger.getLogger(HiberAspect.class);
	private static final boolean openLog = "true".equals(SysConfig.getProperty("open_log"));
	private static final long serialVersionUID = 1L;
	private LogService logService = null;

	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		if (entity instanceof LogEntity) {
			return true;
		}
		Client client = null;
		try {
			client = ClientManager.getInstance().getClientSession();
		} catch (Exception e) {
			logger.error("获取用户Session失败");
		}
		for (int index = 0; index < propertyNames.length; index++) {
			if ("createDate".equals(propertyNames[index])) {
				if (state[index] == null) {
					state[index] = new Date();
				}
				continue;
			} else if (client != null && "creator".equals(propertyNames[index])) {
				if (state[index] == null) {
					state[index] = client.getUser().getName();
				}
				continue;
			} else if (client != null && "depCode".equals(propertyNames[index])) {
				if (state[index] == null && client.getDepart() != null) {
					state[index] = client.getDepart().getDepartCode();
				}
				continue;
			} else if (client != null && "userCode".equals(propertyNames[index])) {
				if (state[index] == null) {
					state[index] = client.getUser().getId();
				}
				continue;
			}
		}
		this.optLog(entity.getClass().getName(), id, 1);
		return true;
	}

	@Override
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
		Client client = null;
		try {
			client = ClientManager.getInstance().getClientSession();
		} catch (Exception e) {
			logger.error("获取用户Session失败");
		}
		for (int index = 0; index < propertyNames.length; index++) {
			if ("updateDate".equals(propertyNames[index])) {
				currentState[index] = new Date();
				continue;
			} else if (client != null && "editor".equals(propertyNames[index])) {
				currentState[index] = client.getUser().getName();
				continue;
			} else if (client != null && "depCode".equals(propertyNames[index])) {
				if (currentState[index] == null && client.getDepart() != null) {
					currentState[index] = client.getDepart().getDepartCode();
				}
				continue;
			} else if (client != null && "userCode".equals(propertyNames[index])) {
				if (currentState[index] == null) {
					currentState[index] = client.getUser().getId();
				}
				continue;
			}
		}

		this.optLog(entity.getClass().getName(), id, 2);

		return true;
	}

	@Override
	public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		this.optLog(entity.getClass().getName(), id, 3);
	}

	/**
	 * 操作日志记录
	 * 
	 * @author WU
	 * @date 2016年4月1日 下午10:33:08
	 * @param entityName
	 * @param id
	 * @param type
	 */
	private void optLog(String entityName, Serializable id, Integer type) {
		if (openLog) {
			if (logService == null) {
				logService = (LogService) SpringContextUtil.getService("logService");
			}
			LogEntity log = new LogEntity();
			log.setOptEntity(entityName);
			log.setOptId(id.toString());
			log.setOptType(type);
			logService.optLog(log);
		}
	}
}
